﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace FourKill.Engine
{
    public class Alert : IPaintable
    {
        // In the resources folder -- needs to be installed into fonts.
        FontFamily family = new FontFamily("Arakawa");
        private int _alpha = 255;
        private int _fadeStrength = 10;
        private FadeDirection _fadeDirection = FadeDirection.Out;
        private Timer fadeTimer = new Timer();

        public Alert() 
        {
            fadeTimer.Interval = 100;
            fadeTimer.Enabled = true;
            fadeTimer.Tick += new EventHandler(fadeTimer_Tick);
        }

        void fadeTimer_Tick(object sender, EventArgs e)
        {
            if (_fadeDirection == FadeDirection.Out)
            {
                _alpha = (_alpha - _fadeStrength > 0) ? _alpha - _fadeStrength : 0;
            }
            else
            {
                _alpha = (_alpha + _fadeStrength < 256) ? _alpha + _fadeStrength : 255;
            }
            if (_alpha == 0) _fadeDirection = FadeDirection.In;
            if (_alpha == 255) _fadeDirection = FadeDirection.Out;
        }


        public void Paint(IPaintableContext context)
        {
            try
            {
                int w = context.ViewPort.PixelSize.Width, 
                    h = context.ViewPort.PixelSize.Height;

                using(Font font = new Font("Arakawa", Convert.ToInt32(w * .15)))
                {
                    using (GraphicsPath gp = new GraphicsPath())
                    {
                        string stringText = "wr!";
                        var size = context.FrontLayer.MeasureString(stringText, font);

                        int fontStyle = (int)FontStyle.Regular;
                        int emSize = (int)(w * .2);
                        Point origin = new Point((int)(w / 2 - size.Width / 2), (int)(h / 2 - size.Height / 2));
                        StringFormat format = StringFormat.GenericDefault;

                        gp.AddString(stringText,
                            family,
                            fontStyle,
                            emSize,
                            origin,
                            format);

                        Color primary = Color.FromArgb(_alpha, Color.Fuchsia);
                        Color secondary = Color.FromArgb(_alpha, Color.Cyan);
                        Color outline = Color.FromArgb(_alpha, Color.Azure);
                        using (var lb = new LinearGradientBrush(new Rectangle(0, 0, (int)(size.Width * 1.4), (int)size.Height), primary, secondary, 0, true))
                        {
                            context.FrontLayer.SmoothingMode = SmoothingMode.AntiAlias;
                            context.FrontLayer.FillPath(lb, gp);
                            context.FrontLayer.DrawPath(new Pen(outline, 3), gp);
                        }
                        format.Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("***********************************");
                System.Diagnostics.Debug.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
                System.Diagnostics.Debugger.Break();
            }
        }
    }
}
